<?
error_reporting(E_ERROR);

// include needed filed if necessary
$tmp = "config.inc.php";
include($tmp);

if (!isset($cfg) || $cfg['httpd_mode'] != 'webcp') {
	$tmp = "functions.inc.phps";
	include($tmp);
}

//
// Setup Database Connection
if ($cfg['httpd_mode'] == 'apache') {
	mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass'], true);
	mysql_select_db($cfg['dbname']);
}

// Load Input Vars:
	$data = load_input_vars('data');
	$step = intval(load_input_vars('step'));

// load default language
include("lang/".$cfg['defaultlang'].".phps");
// can't connect to database server
if (!@mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass'], true)) {
	//echo "<div align='center'>Error: web-cp is not configured correctly. Please check your settings.</div></body></html>";
	send_header('Location: http://'.$_SERVER['HTTP_HOST'].':'.$cfg['port']."/setup_config.php");
	return false;
}
// database doesnt exist
if (!$cfg['initscriptslocated'] || !$cfg['programslocated'] || !$cfg['serversselected'] || !$cfg['modulesselected'] || !@mysql_select_db($cfg['dbname'])) {
	//echo "<div align='center'>Error: You must create the web-cp database before you can continue.</div></body></html>";
	send_header('Location: http://'.$_SERVER['HTTP_HOST'].':'.$cfg['port']."/setup_config.php");
	return false;
}
// database exists but tables haven't been imported
$dbp = mysql_query("SHOW TABLES FROM ".$cfg['dbname']);
if (mysql_num_rows($dbp) === 0) {
	//echo "<div align='center'>Error:  You must import the web-cp sql file before you can continue.</div></body></html>";
	send_header('Location: http://'.$_SERVER['HTTP_HOST'].':'.$cfg['port']."/setup_config.php");
	return false;
}
// If there are any users set already, die.
$dbp = mysql_query("SELECT username FROM users LIMIT 1");
if (mysql_num_rows($dbp)) {
	//echo "<div align='center'>Error:  You cannot run the setup script once a user has been created; setup aborted.</div></body></html>";
	send_header('Location: http://'.$_SERVER['HTTP_HOST'].':'.$cfg['port']."/");
	return false;
}

// If NAT is on use the IP from it.
if ($cfg['NAT']) {

for ($map = 1; $map <= 6; $map++) {
	if ($cfg['NAT-map'.$map] != '') {
		$NATmap = explode(',', $cfg['NAT-map'.$map]);
	}
	$mappedips[] = $NATmap[1];
}
unset($NATmap);
}

$cp = 'server';
$url = 'newaccount';
$current_url = 'setup.php';
// reload default language
include("lang/".$cfg['defaultlang'].".phps");
// load and parse default skin
include("parser.inc.phps");
// If data is set (i.e the submit button has been hit)
if (isset($data)) {
	if (is_array($data)) {
		// remove spaces and slashes.
		while (list($key, $val) = each($data)) {
			if ($key != 'serverside')
				$data[$key] = trim(stripslashes($val));
		}

		$error = false;

		switch(intval($step)) {
			case 6:
				$error = reseller("create",$data,$return);
				sleep(2);
				if (!$error) {
					$data['id'] = $return;
				}
				break;
			case 7:
			// if domain is set, attempt to create domain
				if (ereg("[:space:]",$data['ip'])) {
					$ip = split("([[:space:]]+,?|[[:space:]]*,)",$data['ip']);
					$data['ip'] = $ip[0];
				}
				$data['ip_addr'] = $data['ip'];
				unset($tmp);
				foreach($data['serverside'] AS $key=>$val) {
					if ($val == 'on')
						$tmp[$key] = 'off';
					else
						$tmp[$key] = 'na';
				}
				$data['serverside'] = $tmp;
				$data['users'] = $data['domusers'];
				$data['hd'] = $data['domhd'];
				$data['transfer'] = $data['domtransfer'];
				$data['subdomains'] = $data['domsubdomains'];
				$data['pointers'] = $data['dompointers'];
				$data['databases'] = $data['domdatabases'];
				$data['mailinglists'] = $data['dommailinglists'];
				$data['bandwidth'] = $data['dombandwidth'];
				$data['defaultroot'] = "/".$data['hostname']."/".$cfg['webname']."/%USER%";
				$data['type'] = 'domain';
				$error = domain("create",$data,$return);
				sleep(2);
				if (!$error) {
					$data['id'] = $return;
				}
				break;
			case 8:
			// if username is set, attempt to create user
				if ($data['username']) {
					// ** HACK ** This sleep code should be replaced with a cycle to check for creation of the domain with a reasonable timeout
					// sleep 4 seconds to let the domain be created
					sleep(4);

					$data['aliases'] = $data['usraliases'];
					$data['type'] = 'standard';
					$data['level'] = 0;
					unset($data['root']);

					$error = user("create",$data);
					sleep(2);
					if (!$error) {
						$data['id'] = $return;

						//all done!
						send_header('Location: http://'.$_SERVER['HTTP_HOST'].':'.$cfg['port'].'/');
						return;
					}
				}
				break;
		}
		// Reload page
		//send_header('Location: http://'.$_SERVER['HTTP_HOST'].':'.$cfg['port']);
		if(!$error) {
			$step++;
		}
	}
}

switch($step) {
	case 6:
		$SQL = 'DELETE FROM resellers';
		mysql_query($SQL);
	case 7:
		$SQL = 'DELETE FROM domains';
		mysql_query($SQL);
		break;
}

// If there are any resellers set.
$dbp = mysql_query("SELECT name FROM resellers");
if (mysql_num_rows($dbp)) {
	$dbp = mysql_query("SELECT owner FROM domains");
	if (mysql_num_rows($dbp)) {
		$step = 8;
	} else {
		$step = 7;
	}
} else {
	$step = 6;
}
?>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>web-cp setup script</title>
<meta name="description" content="web-cp setup script">
<style type="text/css">
<!--
	/* Body Style */
	body	{ font-family: Arial;  font-size:16px; margin: 0; background-color:#F5F5F5; }
	/* <a> Links Style */
	a	{ text-decoration:underline;
		color:#2d2d2d;
	}
	a:hover	{ text-decoration:underline;
		color:#464646;
	}
	/* web-cp use the following classes */
	/* Font Style & Types */
	.small	{ font-size:11px; }
	.normal	{ font-size:14px; }
	.big	{ font-size:16px; }
	/* Table Style */
	.tbltop { background-color: #5B7B9C; color: #f5f5f5; font-size: 14px; font-weight: bold; text-align: center; }
	.row1		{ background-color: #e9e9e9; font-size: 11px; text-align: right;}
	.row2		{ background-color: #E2EAF2; font-size: 11px; text-align: left;}
	.tblbg {background-color: #DADADA}
-->
</style>
<script language='JavaScript' src='script.js'></script>
</head>
<body><br>
<div align='center'><big>web-cp setup script</big></div><br>
<table align=center><tr>
	<td class="row1">&nbsp;<a href="setup_config.php?step=1">Server Configuration</a>&nbsp;</td>
	<td> <img src="/icon/rightarrow.gif" width="24" height="24"> </td>
	<td class="row1">&nbsp;<a href="setup_config.php?step=2">Select Servers</a>&nbsp;</td>
	<td> <img src="/icon/rightarrow.gif" width="24" height="24"> </td>
	<td class="row1">&nbsp;<a href="setup_config.php?step=3">Locate Programs</a>&nbsp;</td>
	<td> <img src="/icon/rightarrow.gif" width="24" height="24"> </td>
	<td class="row1">&nbsp;<a href="setup_config.php?step=5">Database Configuration</a>&nbsp;</td>
	<td> <img src="/icon/rightarrow.gif" width="24" height="24"> </td>
	<td class="row1">&nbsp;<a href="setup_config.php?step=6">Select Modules</a>&nbsp;</td>
	<td> <img src="/icon/rightarrow.gif" width="24" height="24"> </td>
	<td <?=(($step == 6) ? 'class="tbltop"' : 'class="row1"');?>>&nbsp;<?=(($step > 6) ? '<a href="setup.php?step=6">' : '');?>Primary Reseller</a>&nbsp;</td>
	<td> <img src="/icon/rightarrow.gif" width="24" height="24"> </td>
	<td <?=(($step == 7) ? 'class="tbltop"' : (($step > 7) ? 'class="row1"' : 'class="row2"'));?>>&nbsp;<?=(($step > 7) ? '<a href="setup.php?step=7">' : '');?>Primary Domain</a>&nbsp;</td>
	<td> <img src="/icon/rightarrow.gif" width="24" height="24"> </td>
	<td <?=(($step == 8) ? 'class="tbltop"' : 'class="row2"');?>>&nbsp;Administrative Account&nbsp;</td>
</tr></table>
<?
if ($error) {
	echo "<center><br>&nbsp;&nbsp; ".implode("<br>\n&nbsp;&nbsp; ",$error)."<br>\n</center>";
}
?>
<br>
<form action="<?=$current_url;?>" method="post" name="webcp" onSubmit="submitonce(this);">
<?
	switch ($step) {
		case 6:
?>
<!-- RESELLER CREATION -->
<input type=hidden name="step" value="6">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellpadding="0" cellspacing="0" class="tblbg"><tr><td>
<table border="0" cellspacing="1">
	<tr>
		<td colspan="2" class="tbltop">&nbsp;Primary Reseller Account&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Name'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[name]" size="25" maxlength="255" value="<? !empty($data) && print $data['name'];?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Email'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[email]" size="25" maxlength="255" value="<? !empty($data) && print $data['email'];?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['IP Address'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<textarea name="data[ip]" cols="19" rows="3" wrap="virtual"><?if (!empty($data) && $data['ip']) echo $data['ip']; elseif ($cfg['NAT'] == true) echo $mappedips[0]; else echo $_SERVER['SERVER_ADDR'];?></textarea>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Domains'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[domains]" size="5" maxlength="255" value="<? if(!empty($data) && $data['domains']) echo intval($data['domains']); else echo 25;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Subdomains'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[subdomains]" size="5" maxlength="255" value="<? if(!empty($data) && $data['subdomains']) echo intval($data['subdomains']); else echo 150;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Pointers'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[pointers]" size="5" maxlength="255" value="<? if(!empty($data) && $data['pointers']) echo intval($data['pointers']); else echo 50;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['HD Quota'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[hd]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['hd'])) echo intval($data['hd']); else echo 2500;?>"> <?=$T['mb'];?>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Users'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[users]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['users'])) echo intval($data['users']); else echo 500;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Databases'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[databases]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['databases'])) echo intval($data['databases']); else echo 50;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Mailing Lists'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[mailinglists]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['mailinglists'])) echo intval($data['mailinglists']); else echo 50;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Transfer'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[transfer]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['transfer'])) echo intval($data['transfer']); else echo 10;?>"> <?=$T['gb'];?>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Shell Access'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="radio" name="data[shell]" value="on"> <?=$T['on'];?>&nbsp;&nbsp;<input type="radio" name="data[shell]" value="off" checked> <?=$T['off'];?>&nbsp;</td>
	</tr>
	<? if ($cfg['bandwidth']) { ?>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Bandwidth'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[bandwidth]" size="5" maxlength="255" value="<? if($data['bandwidth']) echo intval($data['bandwidth']); else echo '0';?>"> <?=$T['kbps'];?>&nbsp;</td>
	</tr>
	<? } ?>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Server Side Options'];?>&nbsp;</b></td>
		<td class="row2"><table border="0" cellpadding="0" cellspacing="0">
		<?
		if (strstr($cfg['sslang'],",")) $sslang = explode(",",$cfg['sslang']);
		else $sslang[0] = $cfg['sslang'];
		$j = 0;
		while (isset($sslang[$j])) {
			unset($check);
			 if (!empty($data['serverside'][trim($sslang[$j])]) && $data['serverside'][trim($sslang[$j])]=="on"){
				$check['on'] = ' checked';
				$check['off'] = '';
			}
			else {
				$check['off'] = ' checked';
				$check['on'] = '';
			}
			echo "<tr><td class='small'>&nbsp;<b>".$T['ss'][trim($sslang[$j])]."</b>&nbsp;</td><td class='small'>\n";
			echo '&nbsp;<input type="radio" name="data[serverside]['.trim($sslang[$j]).']" value="on"'.$check['on'].'>'.$T['on'];
			echo '&nbsp;<input type="radio" name="data[serverside]['.trim($sslang[$j]).']" value="off"'.$check['off'].'>'.$T['off'];
			echo "\n</td></tr>\n";
			++$j;
		}
		?></table></tr>
</table>
</td></tr></table><br>
<?	break;
	case 7:

	if ($data['id'] == "") {
		$SQL = "SELECT id, name, ip, email FROM resellers";
		$res = mysql_query($SQL);
		$row = mysql_fetch_array($res);
		$data['id'] = $row['id'];
		$data['ip'] = trim($row['ip']);
		$data['email'] = $row['email'];
	} else {
		$SQL = "SELECT name, ip, email FROM resellers WHERE id = '".$data['id']."'";
		$res = mysql_query($SQL);
		$row = mysql_fetch_array($res);
		$data['ip'] = trim($row['ip']);
		$data['email'] = $row['email'];
	}
?>
<!-- DOMAIN CREATION -->
<input type="hidden" name="data[email]" size="25" maxlength="255" value="<? echo $data['email']; ?>">
<input type="hidden" name="data[ip]" value="<? echo $data['ip']; ?>">
<input type="hidden" name="data[id]" size="5" maxlength="255" value="<? echo $data['id']; ?>">
<input type="hidden" name="step" value="7">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellpadding="0" cellspacing="0" class="tblbg"><tr><td>
<table border="0" cellspacing="1">
	<tr>
		<td colspan="2" class="tbltop">&nbsp;<?=$T['reseller']['newdomain'];?>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Hostname'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[hostname]" size="25" maxlength="255" value="<? if(empty($data) || !$data['hostname']) echo "www"; else echo $data['hostname'];?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Domain Name'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[domain]" size="25" maxlength="255" value="<? if(!empty($data) && $data['domain']) print $data['domain'];?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Users'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[domusers]" size="5" maxlength="255" value="<? if(!empty($data) && $data['users']) echo intval($data['users']); else echo 10;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['HD Quota'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[domhd]" size="5" maxlength="255" value="<? if(!empty($data) && $data['hd']) echo intval($data['hd']); else echo 50;?>"> <?=$T['mb'];?>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Transfer Quota'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[domtransfer]" size="5" maxlength="255" value="<? if(!empty($data) && $data['domtransfer']) echo intval($data['domtransfer']); else echo 2;?>"> <?=$T['gb'];?>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Subdomains'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[domsubdomains]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['domsubdomains'])) echo intval($data['domsubdomains']); else echo 2;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Pointers'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[dompointers]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['dompointers'])) echo intval($data['dompointers']); else echo 1;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Databases'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[domdatabases]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['domdatabases'])) echo intval($data['domdatabases']); else echo 5;?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Num Mailing Lists'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[dommailinglists]" size="5" maxlength="255" value="<? if(!empty($data) && isset($data['dommailinglists'])) echo intval($data['dommailinglists']); else echo 50;?>">&nbsp;</td>
	</tr>
	<? if ($cfg['bandwidth']) { ?>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Bandwidth'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[dombandwidth]" size="5" maxlength="255" value="<? if(!empty($data) && $data['dombandwidth']) echo intval($data['dombandwidth']); else echo '0';?>"> <?=$T['kbps'];?>&nbsp;</td>
	</tr>
	<? } ?>
</table>
</td></tr></table><br>
<?	break;
	case 8:
		$res = mysql_query("SELECT domains.id as domainid, resellers.name as resellername FROM domains inner join resellers on domains.owner = resellers.id");
		$row = mysql_fetch_array($res);

		$data['id'] = $row['domainid'];
		if (!isset($data['name']) || $data['name'] == '') { $data['name'] = $row['resellername'];}
?>
<!-- USER CREATION -->
<input type="hidden" name="data[id]" value="<?=$data['id'];?>">
<input type="hidden" name="data[hd]" value="50">
<input type=hidden name="step" value="8">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellpadding="0" cellspacing="0" class="tblbg"><tr><td>
<table border="0" cellspacing="1">
	<tr>
		<td colspan="2" class="tbltop">&nbsp;<?=$T['menu'][$url];?>&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Name'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[name]" size="25" maxlength="50" value="<? if(!empty($data) && $data['name']) echo $data['name'];?>">&nbsp;</td>
	</tr>

	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Username'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[username]" size="25" maxlength="255" value="<?if(!empty($data) && $data['username']) echo $data['username']; else echo 'admin';?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['Password'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<input type="text" name="data[password]" size="25" maxlength="255" value="<?if(!empty($data) && $data['password']) print $data['password'];?>">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1"><b>&nbsp;<?=$T['E-Mail Aliases'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<textarea name="data[usraliases]" cols="20" rows="3" wrap="virtual"><?if (!empty($data) && $data['usraliases']) echo trim($data['usraliases']); else echo 'admin, root';?></textarea>&nbsp;</td>
	</tr>
	<tr>
		<? // get available languages
		if (strstr($cfg['lang'],",")) {
			$language = explode(",",$cfg['lang']);
		}
		else $language[1] = $cfg['lang'];
		?>
		<td class="row1"><b>&nbsp;<?=$T['Language'];?>&nbsp;</b></td>
		<td class="row2">&nbsp;<select name="data[lang]">
		<? do {
			echo '<option value="'.trim(current($language)).'"';
			if (current($language) == $userdata['lang']) echo " SELECTED";
			echo '> '.$T[current($language)].'</option>';
			echo "\n";
		} while(next($language)); ?>
           	</select>&nbsp;</td>
	</tr>
</table>
</td></tr></table><br>
<?	break;
}
?>
<div align="right"><input type="submit" value="Submit"></div>
</td></tr></table>
</form>
</div>
</body>
</html>
